/**
 * User: yuhao
 * Date: 15-8-26
 * Time: 上午9:21
 * 图层常用操作类
 */

define(
    [
        "require",
        "dojo/_base/declare",
        "dojo/_base/lang",
        "dojo/_base/array",
        "dojo/on",
        "dojo/topic",
        "esri/layers/GraphicsLayer",
        "esri/graphic",
        "esri/geometry/Point",
        "esri/SpatialReference",
        "esri/geometry/geometryEngine",
        "../util/renderUtil",
        "../util/utils"
    ],
    function (require, declare, lang, array, on, topic, GraphicsLayer, Graphic, Point, SpatialReference, geometryEngine, renderUtil, utils) {

        return {

            /**
             * 使用oss的json数据，创建GraphicsLayer
             * @param dataConfig
             * @param layerConfig
             * @param dataContent
             * @return {*}
             */
            createGraphicsLayerFromOss:function (map, dataConfig, layerConfig, dataContent) {
                if (!dataConfig) {
                    return null;
                }
                //设置图层id
                var layer = new GraphicsLayer({id:layerConfig.id});

                //设置render
                if (dataContent.renderer) {
                    var style = layerConfig.cfg.style,
                        renderer = renderUtil.getFillRenderer({
                            symbol:style.symbol,
                            opacity:style.opacity
                        });
                    layer.setRenderer(renderer);
                }

                //添加要素
                array.forEach(dataContent.graphics, function (g) {
                    layer.add(new Graphic(g));
                });

                //注册click事件
                on(layer, "click", lang.hitch(this, function (evt) {
                    evt.stopPropagation();

                    var features = array.filter(layer.graphics, function (feature) {
                        return geometryEngine.contains(evt.graphic.geometry, feature.geometry);
                    }, this);

                    var popupConfig = {
                        map:map,
                        features:features,
                        fields:dataConfig.cfg.fields,
                        position:evt.mapPoint,
                        subject:"信息"
                    };
                    var popup = layerConfig.cfg.popup;
                    lang.mixin(popupConfig, popup);
                    topic.publish("openMapPopup", popupConfig);
                }));

                return layer;
            }

        };
    });